package org.p2psimulator.entity.behavior;

import java.util.Random;

import org.p2psimulator.entity.Id;
import org.p2psimulator.entity.Message;
import org.p2psimulator.entity.reputation.Reputation;
import org.p2psimulator.entity.reputation.ReputationRepository;
import org.p2psimulator.exceptions.ReputationRepositoryException;

public class MaliciousBehavior extends Behavior {

	private static Random r = new Random();
	@Override
	public boolean answareMessage(Id rater, Message msg) {
	interacitonCount++;
            return true;
	}

	@Override
	public BehaviorTypes getBehaviorType() {
		return BehaviorTypes.MALICIOUS;
	}

	@Override
	public boolean rate() {
		return true;
	}
	@Override
	public double ratePeer(Id rater, Id ratee, BehaviorTypes rateeBehavior,
			ReputationRepository repository) {

		try {
			Reputation reputation = repository.getLocalReputation(rater, ratee);

			if (reputation != null){
				if (reputation.getAverageReputation() > 0){
					return -1;
				}else{
					return 1;
				}
			}else{
				return 0;
			}
		} catch (ReputationRepositoryException e) {
			e.printStackTrace();
			return 0;
		}


	}

	@Override
	public double rateResource(Id rater, Id resource,
			ReputationRepository repository) {
		try {
			Reputation reputation = repository.getLocalReputation(rater, resource);

			if (reputation != null){
				if (reputation.getNormalReputation() > 0){
					return -1;
				}else{
					return 1;
				}
			}else{
				return 0;
			}

		} catch (ReputationRepositoryException e) {
			e.printStackTrace();
			return 0;
		}
	}

	@Override
	public double rateTransaction(Id rater, Id ratee, int transResult,
			BehaviorTypes rateeBehavior, ReputationRepository repository) {
	interacitonCount++;
            return transResult > 0 ? -1:1;
	}

	public boolean leaveNetwork() {
		return true;
	}

	@Override
	public boolean damageResource() {
		// TODO Auto-generated method stub
		int t = r.nextInt(101);

		if (t < 90){
			return true;
		}else{
			return false;
		}
	}

	@Override
	public boolean transacNeutro(BehaviorTypes rateeBehavior, Id ratee) {
		return false;
	}

}
